<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="买家ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyerId">
              <j-search-select-tag v-model="model.buyerId" dict="sys_user,username,id"  />
            </a-form-model-item>
          </a-col>
          <address-form ref="addressForm" :showCaptcha="false" :showCheckbox="false"> </address-form>

          <!-- <a-col :span="24">
            <a-form-model-item label="国家/地区ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="regionId">
              <a-input v-model="model.regionId" placeholder="请输入国家/地区ID"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="省级ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="provinceId">
              <j-dict-select-tag type="list" v-model="model.provinceId" dictCode="ht_area,name,id" placeholder="请选择省级ID" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="市级ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityId">
              <j-dict-select-tag type="list" v-model="model.cityId" dictCode="ht_area,name,id" placeholder="请选择市级ID" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="区ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="districtId">
              <j-dict-select-tag type="list" v-model="model.districtId" dictCode="ht_area,name,id" placeholder="请选择区ID" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="街道地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="streetAddress">
              <a-textarea v-model="model.streetAddress" rows="4" placeholder="请输入街道地址" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="楼/门牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="suiteAddress">
              <a-textarea v-model="model.suiteAddress" rows="4" placeholder="请输入楼/门牌号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactPerson">
              <a-input v-model="model.contactPerson" placeholder="请输入联系人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phoneNumber">
              <a-input v-model="model.phoneNumber" placeholder="请输入手机电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="座机电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="telephone">
              <a-input v-model="model.telephone" placeholder="请输入座机电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入邮箱"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipcode">
              <a-input-number v-model="model.zipcode" placeholder="请输入邮编" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否默认地址：0-非默认地址，1-默认地址。" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isDefault">
              <j-switch v-model="model.isDefault"  ></j-switch>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import AddressForm from '@/components/htxincailiao/address/AddressFrom.vue'

  export default {
    name: 'HTBuyerAddressForm',
    components: {
      AddressForm
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           buyerId: [
              { required: true, message: '请输入买家ID!'},
           ],
           regionId: [
              { required: true, message: '请输入国家/地区ID!'},
           ],
           provinceId: [
              { required: true, message: '请输入省级ID!'},
           ],
           cityId: [
              { required: true, message: '请输入市级ID!'},
           ],
           districtId: [
              { required: true, message: '请输入区ID!'},
           ],
           streetAddress: [
              { required: true, message: '请输入街道地址!'},
           ],
           suiteAddress: [
              { required: true, message: '请输入楼/门牌号!'},
           ],
           contactPerson: [
              { required: true, message: '请输入联系人!'},
           ],
           phoneNumber: [
              { required: true, message: '请输入手机电话!'},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
           ],
           email: [
              { required: false},
              { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的邮箱!'},
           ],
           zipcode: [
              { required: false},
              { pattern: /^[1-9]\d{5}$/, message: '请输入正确的邮政编码!'},
           ],
           isDefault: [
              { required: true, message: '请输入是否默认地址：0-非默认地址，1-默认地址。!'},
           ],
        },
        url: {
          add: "/address/hTBuyerAddress/add",
          edit: "/address/hTBuyerAddress/edit",
          queryById: "/address/hTBuyerAddress/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>
